<?php
/**
* AdminController类
*/
class AdminController extends Controller
{
	//加载后台主页面
	public function index(){
		$this->display("admin/index.html");
	}
	
	//加载欢迎页面
	public function welcome(){
		$this->display("admin/welcome.html");
	}

/*
 * 处理文章
 *
 */

	//加载文章列表
	public function art(){
		$mod = new Model("blog");

		 //判断并封装搜索条件
          if(!empty($_POST['name'])){
                $mod->where("title like '%{$_POST['name']}%'");
          }
          
          if(!(empty($_POST['start'])||empty($_POST['stop']))){
          	$start = !empty($_POST['start'])?strtotime($_POST['start']):strtotime("1995-01-01"); 
          	$stop = !empty($_POST['stop'])?strtotime($_POST['stop']):strtotime("now"); 
          	 $mod->where("addtime between {$start} and {$stop} ");
          }
        $count = $mod->count();
        $p = new Page($count,4);
		$this->assign("list",$mod->limit($p->limit())->select());
		$this->assign("show",$p->show());
		$this->assign("count",$count);
		$this->display("admin/artList.html");
	}

	//加载文章添加
	public function artadd(){
		$this->display("admin/artAdd.html");
	}
	//执行文章添加
	public function artInsert(){
		$mod = new Model("blog");
		$_POST['addtime'] = time();
		$_POST['author'] = "静候花开";
		$_POST['pic'] = '11.jpg';
		$_POST['clicks'] = 0;
		$m = $mod->insert();
		if($m>0){
            $this->art();
        }
	}
	//执行文章删除
	public function artDel(){
		$mod = new Model("blog");
		$m = $mod->del($_GET['id']);
		echo $m;
	}
	//执行文章的批量删除
	public function artDelAll(){
		$mod = new Model("blog");
		$m = $mod->del($_GET['id']);
		if($m>0){
			echo $_GET['id'];
		}else{
			echo 0;
		}
	}
	//加载文章编辑页
	public function artEdit(){
		$mod = new Model("blog");
		$this->assign("art",$mod->find($_GET['id']));
		$this->display("admin/artEdit.html");
	}
	//执行页面编辑
	public function artUpdate(){
		$mod = new Model("blog");
		$m = $mod->update();
		if ($m>0) {
			$this->art();
		}else{

		}
	}

/*
 * 处理相册
 *
 */

	//加载相册
	public function photo(){
		$this->display("admin/photoList.html");
	}

	//加载相册添加
	public function photoadd(){
		$this->display("admin/photoAdd.html");
	}

	//加载相册添加
	public function photoEdit(){
		$this->display("admin/photoEdit.html");
	}

/*
 * 处理留言板
 *
 */
	
	//加载留言板	
	public function messageBoard(){
		$this->display("admin/messageBoard.html");
	}

/*
 * 处理分类
 *
 */

	//加载分类
	public function category(){
		$mod = new Model("kind");
		$count = $mod->count();
        $p = new Page($count,6);
		$this->assign("kind",$mod->limit($p->limit())->select());
		$this->assign("show",$p->show());
		$this->assign("count",$count);
		$this->display("admin/categoryList.html");
	}
	//添加分类
	public function categoryInsert(){
		$mod = new Model("kind");
		$_POST['state'] = 1;
		$_POST['addtime'] = time();
		$m = $mod->insert();
		if ($m>0) {
			$this->category();
		}

	}
	//删除分类
	public function categoryDel(){
		$mod = new Model("kind");
		$m = $mod->del($_GET['id']);
		echo $m;
	}
	//加载编辑页面
	public function cateEdit(){
		$mod = new Model("kind");
		$this->assign("cate",$mod->find($_GET['id']));
		$this->display("admin/cateEdit.html");
	}
	//执行分类修改
	public function cateUpdate(){
		$mod = new Model("kind");
		$m = $mod->update();
		if ($m>0) {
			$this->category();
		}
	}

/*
 *处理标签
 *
 */

	//加载标签
	public function tag(){
		$mod = new Model("tag");
		$count = $mod->count();
        $p = new Page($count,6);
		$this->assign("tag",$mod->limit($p->limit())->select());
		$this->assign("show",$p->show());
		$this->assign("count",$count);
		$this->display("admin/tagList.html");
	}
	//添加标签
	public function tagInsert(){
		$mod = new Model("tag");
		$_POST['state'] = 1;
		$_POST['addtime'] = time();
		$m = $mod->insert();
		if ($m>0) {
			$this->tag();
		}

	}
	//删除分类
	public function tagDel(){
		$mod = new Model("tag");
		$m = $mod->del($_GET['id']);
		echo $m;
	}
	//加载编辑页面
	public function tagEdit(){
		$mod = new Model("tag");
		$this->assign("tag",$mod->find($_GET['id']));
		$this->display("admin/tagEdit.html");
	}
	//执行分类修改
	public function tagUpdate(){
		$mod = new Model("tag");
		$m = $mod->update();
		if ($m>0) {
			$this->tag();
		}
	}

/*
 *处理轮播图
 *
 */

	//加载轮播图列表
	public function lunbo(){

		$this->display("admin/bannerList.html");
	}

	//加载轮播图添加
	public function bannerAdd(){
		$this->display("admin/bannerAdd.html");
	}

	//加载轮播图编辑
	public function bannerEdit(){
		$this->display("admin/bannerEdit.html");
	}

/*
 *处理评论
 *
 */

	//加载评论列表
	public function comment(){
		$mod = new Model("comment");
		$mod2 = new Model("blog");
		$mod3 = new Model("users");
		$count = $mod->count();
        $p = new Page($count,6);
        $m = $mod->limit($p->limit())->select();
        foreach($m as $k=>$v){
        	$t = $mod2->find($v['aid']);
        	$u = $mod3->find($v['uid']);
        	$m[$k]['title'] = $t['title'];
        	$m[$k]['user'] = $u['username'];
        }
		$this->assign("com",$m);
		$this->assign("show",$p->show());
		$this->assign("count",$count);
		$this->display("admin/commentList.html");
	}
	//删除评论
	public function comDel(){
		$mod = new Model("comment");
		$m = $mod->del($_GET['id']);
		echo $m;
	}


/*
 *处理会员
 *
 */
	//加载会员列表页
	public function memberList(){
		$mod = new Model("users");
		 //判断并封装搜索条件
          if(!empty($_POST['name'])){
                $mod->where("username like '%{$_POST['name']}%'");
          }
          
          if(!(empty($_POST['start'])||empty($_POST['stop']))){
          	$start = !empty($_POST['start'])?strtotime($_POST['start']):strtotime("1995-01-01"); 
          	$stop = !empty($_POST['stop'])?strtotime($_POST['stop']):strtotime("now"); 
          	 $mod->where("addtime between {$start} and {$stop} ");
          }
		$count = $mod->count();
        $p = new Page($count,6);
		$this->assign("mem",$mod->limit($p->limit())->select());
		$this->assign("show",$p->show());
		$this->assign("count",$count);
		$this->display("admin/memberList.html");
	}

	//加载会员添加页
	public function memberAdd(){
		$this->display("admin/memberAdd.html");
	}

	//执行会员添加
	
	
	
	//删除会员
	public function memberDel(){
		$mod = new Model("users");
		$m = $mod->del($_GET['id']);
		echo $m;
	}
	//批量删除会员
	public function memberDelAll(){
		$mod = new Model("users");
		$m = $mod->del($_GET['id']);
		if($m>0){
			echo $_GET['id'];
		}else{
			echo 0;
		}
	}
	//加载会员编辑页
	public function memberEdit(){

		$this->display("admin/memberEdit.html");
	}

	//修改用户密码memberPW
	public function memberPW(){
		$this->display("admin/memberPassword.html");
	}	
	

}